package com.bw.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.user.Entity.UserEntity;
import com.bw.user.controller.UserQueryVo;
import com.bw.user.dao.UserDao;
import com.bw.user.service.UserService;
import com.bw.user.utils.Utils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
*@ClassName impl
*@Author Hao_YunKai
*@Date 2022/4/9 10:51
*/
@Service("UserService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public IPage<UserEntity> list(String userName, String phone, Integer page, Integer size) {
        Page<UserEntity> userPage = new Page<>(page, size);
        return userDao.vagueList(userPage,userName,phone);
    }

    @Override
    public ResponseEntity fileUpload(MultipartFile file) {
        if (!Utils.checkExtension(file)){
            return new ResponseEntity("请求文件类型错误:后缀名错误", HttpStatus.BAD_REQUEST);
        }
        try {
            if (Utils.isOfficeFile(file)){
                //正确的文件类型 自动判断2003或者2007
                Workbook workbook = Utils.getWorkbookAuto(file);
                Sheet sheet = workbook.getSheetAt(0);//默认只有一个sheet
                int rows = sheet.getPhysicalNumberOfRows();//获得sheet有多少行
                //读第一个sheet
                for (int i = 0;i<rows;i++){
                    Row row = sheet.getRow(i);
                    for (int j=0;j<row.getLastCellNum();j++){
                        Cell cell = row.getCell(j);
                        if (cell!=null)
                            System.out.println(cell.toString());
                    }
                }
            }else {
                return new ResponseEntity("请求文件类型错误:文件类型错误",HttpStatus.BAD_REQUEST);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ResponseEntity(HttpStatus.OK);
    }

    @Override
    public void downLoadExcel(HttpServletResponse response) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");

        List<UserEntity> classmateList = userDao.list();

        String fileName = "userinf"  + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据

        int rowNum = 1;

        String[] headers = { "用户编号", "用户姓名", "登录密码", "真实姓名", "邮箱", "手机号", "备注", "冻结状态", "注册日期", "性别", "爱好"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (UserEntity user : classmateList) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(user.getUsersId());
            row1.createCell(1).setCellValue(user.getUserName());
            row1.createCell(2).setCellValue(user.getPassword());
            row1.createCell(3).setCellValue(user.getTrueName());
            row1.createCell(4).setCellValue(user.getEmail());
            row1.createCell(5).setCellValue(user.getPhone());
            row1.createCell(6).setCellValue(user.getComments());
            row1.createCell(7).setCellValue(user.getLockState());
            row1.createCell(8).setCellValue(user.getRegisterDate());
            row1.createCell(9).setCellValue(user.getSex());
            row1.createCell(10).setCellValue(user.getLove());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        try {
            response.flushBuffer();
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}